﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 粒子系统拓展
/// </summary>
public static class ParticleSystemException
{
    public static float GetMaxValue(this ParticleSystem.MinMaxCurve minMaxCurve)
    {
        switch (minMaxCurve.mode)
        {
            case ParticleSystemCurveMode.Constant:
                return minMaxCurve.constant;
            //case ParticleSystemCurveMode.Curve:
            //    return minMaxCurve.curve.GetMaxValue();
            //case ParticleSystemCurveMode.TwoCurves:
            //    var ret1 = minMaxCurve.curveMin.GetMaxValue();
            //    var ret2 = minMaxCurve.curveMax.GetMaxValue();
            //    return ret1 > ret2 ? ret1 : ret2;
            case ParticleSystemCurveMode.TwoConstants:
                return minMaxCurve.constantMax;
           
        }
        return -1f;
    }

    public static float GetDuration(this ParticleSystem particle, bool allowLoop = false)
    {
        if (!particle.emission.enabled) return 0f;
        if (particle.main.loop && !allowLoop)
        {
            return -1f;
        }
        if (particle.emission.rateOverTime.GetMaxValue() <= 0)
        {
            return particle.main.startDelay.GetMaxValue() + particle.main.startLifetime.GetMaxValue();
        }
        else
        {
            return particle.main.startDelay.GetMaxValue() + Mathf.Max(particle.main.duration, particle.main.startLifetime.GetMaxValue());
        }
    }
    /// <summary>
    /// 获取粒子持续时间
    /// </summary>
    /// <param name="gameObject"></param>
    /// <param name="includeChildren">是否包含所有子节点对象</param>
    /// <param name="includeInactive">是否包含非激活对象</param>
    /// <param name="allowLoop">是否允许计算循环特效的时长，如果允许则计算一个周期，不允许则返回-1</param>
    /// <returns></returns>
    public static float GetParticleDuration(this GameObject gameObject, bool includeChildren = true, bool includeInactive = false, bool allowLoop = false)
    {
        if (includeChildren)
        {
            var particles = gameObject.GetComponentsInChildren<ParticleSystem>(includeInactive);
            var duration = -1f;
            for (var i = 0; i < particles.Length; i++)
            {
                var ps = particles[i];
                var time = ps.GetDuration(allowLoop);
                if (time > duration)
                {
                    duration = time;
                }
            }

            return duration;
        }
        else
        {
            var ps = gameObject.GetComponent<ParticleSystem>();
            if (ps != null)
            {
                return ps.GetDuration(allowLoop);
            }
            else
            {
                return -1f;
            }
        }
    }
}
